﻿#include "FTitleBar.h"

#if USE_MAC
#include "ui_FTitleBarMac.h"
#else
#include "ui_FTitleBarWin.h"
#endif

#include <QStyle>
#include <QApplication>
#include <QWindow>
#include <QIcon>
#include <QPixmap>
#include <windows.h>
#include <QtWinExtras/QtWin>   // QtWin::fromHICON

#define ICON_SIZE  QSize(16, 16)

QIcon loadAppIconFromExe()
{
    // 取得当前可执行文件路径
    wchar_t path[MAX_PATH];
    GetModuleFileNameW(nullptr, path, MAX_PATH);

    // 提取图标（这里提取第 0 个图标）
    HICON hIconBig = nullptr;
    HICON hIconSmall = nullptr;
    // ExtractIconExW 可提取多个图标索引，这里取索引 0
    ExtractIconExW(path, 0, &hIconBig, &hIconSmall, 1);

    QIcon result;
    if (hIconBig) {
        QPixmap pm = QtWin::fromHICON(hIconBig);
        result.addPixmap(pm);
        DestroyIcon(hIconBig);
    }
    if (hIconSmall) {
        QPixmap pm = QtWin::fromHICON(hIconSmall);
        result.addPixmap(pm);
        DestroyIcon(hIconSmall);
    }
    return result;
}


FTitleBar::FTitleBar(QWidget *parent)
    : QWidget(parent)
    , ui(new UiFTitleBar)
{
    ui->setupUi(this);

    ui->label->setText(parent->windowTitle());
    ui->btnIcon->setIcon(loadAppIconFromExe());
    // 设置自定义区域 布局方向为从右到左
    ui->layoutCustom->setDirection(QHBoxLayout::RightToLeft);

    m_agent = new QWK::WidgetWindowAgent(parent);
    m_agent->setup(parent);

    m_agent->setTitleBar(this);

#if !USE_MAC
    m_agent->setSystemButton(QWK::WindowAgentBase::WindowIcon, ui->btnIcon);
#endif
    m_agent->setSystemButton(QWK::WindowAgentBase::Minimize, ui->btnMin);
    m_agent->setSystemButton(QWK::WindowAgentBase::Maximize, ui->btnMax);
    m_agent->setSystemButton(QWK::WindowAgentBase::Close, ui->btnClose);

    setBtnStdIcon(ui->btnMin, QStyle::SP_TitleBarMinButton);
    setBtnStdIcon(ui->btnMax, QStyle::SP_TitleBarMaxButton);
    setBtnStdIcon(ui->btnClose, QStyle::SP_TitleBarCloseButton);

    ui->btnIcon->setIconSize(ICON_SIZE);
}

FTitleBar::~FTitleBar()
{
    delete ui;
}

void FTitleBar::setIcon(const QIcon &icon)
{
    ui->btnIcon->setIcon(icon);
}

void FTitleBar::setTitle(const QString &title)
{
    ui->label->setText(title);
}

QPushButton *FTitleBar::addStandardButton(const QIcon &icon)
{
    QPushButton* btn = new QPushButton(this);
    btn->setIcon(icon);
    addStandardButton(btn);
    return btn;
}

QPushButton *FTitleBar::addStandardButton(const QString &text)
{
    QPushButton* btn = new QPushButton(this);
    btn->setText(text);
    addStandardButton(btn);
    return btn;
}

void FTitleBar::addStandardButton(QPushButton *btn)
{
    btn->setMinimumWidth(45);
    btn->setIconSize(ICON_SIZE);
    // 隐藏菜单角标
    btn->setStyleSheet("QPushButton::menu-indicator { subcontrol-position: center center; width: 0px; height: 0px; }");

    QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
    btn->setSizePolicy(sizePolicy);

    ui->layoutCustom->addWidget(btn);
    m_agent->setHitTestVisible(btn, true);
}

void FTitleBar::addCustomWidget(QWidget *wgt)
{
    ui->layoutCustom->addWidget(wgt);
    m_agent->setHitTestVisible(wgt, true);
}

void FTitleBar::setMaxIcon(bool isMaxIcon)
{
    if (isMaxIcon)
        setBtnStdIcon(ui->btnMax, QStyle::SP_TitleBarMaxButton);
    else
        setBtnStdIcon(ui->btnMax, QStyle::SP_TitleBarNormalButton);
}

void FTitleBar::setWidgetVisible(uint16_t wId, bool en)
{
    switch (wId) {
    case DefaultButton::Close:
        ui->btnClose->setVisible(en);
        break;
    case DefaultButton::Max:
        ui->btnMax->setVisible(en);
        break;
    case DefaultButton::Min:
        ui->btnMin->setVisible(en);
        break;
    case DefaultButton::Icon:
        ui->btnIcon->setVisible(en);
        break;
    }
//    if (wId < DefaultButton::_Count) {
//        // 默认按钮区域是从左往右的
//        ui->layoutDefault->itemAt(DefaultButton::_Count - wId - 1)->widget()->setVisible(en);
//    } else {
//        // 自定义区域是从右往左
//        int cId = wId - DefaultButton::_Count;
//        if (cId < ui->layoutCustom->count()) {
//            ui->layoutCustom->itemAt(cId)->widget()->setVisible(en);
//        }
//    }
}

void FTitleBar::onParentMaximized(bool isMax)
{
    setMaxIcon(!isMax);
}

void FTitleBar::setBtnStdIcon(QPushButton *btn, QStyle::StandardPixmap stdIcon)
{
    btn->setIcon(QApplication::style()->standardIcon(stdIcon));
#if USE_MAC
        btn->setIconSize(QSize(8, 8));
#else
        btn->setIconSize(ICON_SIZE);
#endif
}

void FTitleBar::resizeEvent(QResizeEvent *e)
{
    QWidget::resizeEvent(e);
}

void FTitleBar::on_btnMax_clicked()
{
    if (parentWidget()->isMaximized()) {
        parentWidget()->showNormal();
    } else {
        parentWidget()->showMaximized();
    }
}

void FTitleBar::on_btnMin_clicked()
{
    parentWidget()->showMinimized();
}


void FTitleBar::on_btnClose_clicked()
{
    parentWidget()->close();
}

